package o;

import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import androidx.annotation.GuardedBy;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.d;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@RestrictTo({d.ॱ})
public class gw {
    private final int ʻ;
    @GuardedBy("mLock")
    private HandlerThread ˊ;
    @GuardedBy("mLock")
    private Handler ˋ;
    private final Object ˎ = new Object();
    @GuardedBy("mLock")
    private int ˏ;
    private Callback ॱ = new Callback(this) {
        final /* synthetic */ gw ॱ;

        {
            this.ॱ = r1;
        }

        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 0:
                    this.ॱ.ˊ();
                    break;
                case 1:
                    this.ॱ.ˏ((Runnable) message.obj);
                    break;
            }
            return true;
        }
    };
    private final int ॱॱ;
    private final String ᐝ;

    public interface a<T> {
        void ˋ(T t);
    }

    public gw(String str, int i, int i2) {
        this.ᐝ = str;
        this.ॱॱ = i;
        this.ʻ = i2;
        this.ˏ = 0;
    }

    private void ˊ(Runnable runnable) {
        synchronized (this.ˎ) {
            if (this.ˊ == null) {
                this.ˊ = new HandlerThread(this.ᐝ, this.ॱॱ);
                this.ˊ.start();
                this.ˋ = new Handler(this.ˊ.getLooper(), this.ॱ);
                this.ˏ++;
            }
            this.ˋ.removeMessages(0);
            this.ˋ.sendMessage(this.ˋ.obtainMessage(1, runnable));
        }
    }

    public <T> void ॱ(final Callable<T> callable, final a<T> aVar) {
        final Handler handler = new Handler();
        ˊ(new Runnable(this) {
            final /* synthetic */ gw ˊ;

            public void run() {
                Object call;
                try {
                    call = callable.call();
                } catch (Exception e) {
                    call = null;
                }
                handler.post(new Runnable(this) {
                    final /* synthetic */ AnonymousClass3 ˎ;

                    public void run() {
                        aVar.ˋ(call);
                    }
                });
            }
        });
    }

    public <T> T ॱ(Callable<T> callable, int i) throws InterruptedException {
        final ReentrantLock reentrantLock = new ReentrantLock();
        final Condition newCondition = reentrantLock.newCondition();
        final AtomicReference atomicReference = new AtomicReference();
        final AtomicBoolean atomicBoolean = new AtomicBoolean(true);
        final Callable<T> callable2 = callable;
        ˊ(new Runnable(this) {
            final /* synthetic */ gw ᐝ;

            public void run() {
                try {
                    atomicReference.set(callable2.call());
                } catch (Exception e) {
                }
                reentrantLock.lock();
                try {
                    atomicBoolean.set(false);
                    newCondition.signal();
                } finally {
                    reentrantLock.unlock();
                }
            }
        });
        reentrantLock.lock();
        try {
            T t;
            if (atomicBoolean.get()) {
                long toNanos = TimeUnit.MILLISECONDS.toNanos((long) i);
                while (true) {
                    try {
                        toNanos = newCondition.awaitNanos(toNanos);
                    } catch (InterruptedException e) {
                    }
                    if (!atomicBoolean.get()) {
                        break;
                    } else if (toNanos <= 0) {
                        throw new InterruptedException("timeout");
                    }
                }
                t = atomicReference.get();
                reentrantLock.unlock();
            } else {
                t = atomicReference.get();
            }
            return t;
        } finally {
            reentrantLock.unlock();
        }
    }

    void ˏ(Runnable runnable) {
        runnable.run();
        synchronized (this.ˎ) {
            this.ˋ.removeMessages(0);
            this.ˋ.sendMessageDelayed(this.ˋ.obtainMessage(0), (long) this.ʻ);
        }
    }

    void ˊ() {
        synchronized (this.ˎ) {
            if (this.ˋ.hasMessages(1)) {
                return;
            }
            this.ˊ.quit();
            this.ˊ = null;
            this.ˋ = null;
        }
    }
}
